#include "passworddialog.h"
#include "models/command.h"
#include "models/operatelogmodel.h"
#include "ui_passworddialog.h"

#include <models/curusermodel.h>
#include<QDebug>

PasswordDialog::PasswordDialog(QWidget *parent)
    : PopupDialog(parent)
    , ui(new Ui::PasswordDialog)
{
    ui->setupUi(this);
    scaleWidgets(this,g_xScale,g_yScale);
}

PasswordDialog::~PasswordDialog()
{
    delete ui;
}

void PasswordDialog::on_pushButton_clicked()
{
    reject();
}


void PasswordDialog::scaleWidgets(QWidget *widget, double xScale, double yScale)
{
    setFixedSize(int(widget->width() * xScale),int(widget->height() * yScale));
    for (QWidget * child : widget->findChildren<QWidget*>()) {
        QRect geom = child->geometry();
        int width = qMax(1, static_cast<int>(int(geom.width() * xScale)));
        //当按钮高度比较大时使用y方向比例，当按钮比较小时使用x比例，防止按钮被压扁
        int height = qMax(1, static_cast<int>((geom.height() == geom.width() ? int(geom.height() * xScale) : int(geom.height() * yScale))));
        child->setFixedSize(width,height);
        child->move(int(geom.left() * xScale),int(geom.top() * yScale));
        QFont font = child->font();
        if(font.pixelSize()> 0)
            font.setPixelSize(int(font.pixelSize()*xScale));
        else if(font.pointSize()> 0)
            font.setPixelSize(int(font.pointSize()*xScale));
        child->setFont(font);
    }
}


void PasswordDialog::on_ok_btn_clicked()
{
    if(ui->newPwd_edit->text() != ui->confirmPwd_edit->text())
    {
        ui->error_lab->setText(tr("新密码与确认密码不一致！"));
        return;
    }
    QString strErr;
    if(CurUserModel::getInstance()->updatePwd(ui->oldPwd_edit->text(),ui->newPwd_edit->text(),strErr))
    {
        OPERATE_LOG("修改用户密码");
        accept();
    }
    else
    {
        ui->error_lab->setText(strErr);
    }
}


void PasswordDialog::on_cancel_ban_clicked()
{
    reject();
}

